package com.ls.lserp.modules.produce.service;





import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.modules.produce.dao.ProducePlanDao;
import com.ls.lserp.modules.produce.entity.ProducePlan;
import com.ls.lserp.modules.sys.entity.Sequence;
import com.ls.lserp.modules.sys.utils.SequenceUtil;
@Service
@Transactional(readOnly = false)
public class ProducePlanService extends CrudService<ProducePlanDao,ProducePlan>{

	
	
	@Autowired
    private ProducePlanDao producePlanDao ;
	    /**
		 * 查询父项
		 * @param page
		 * @param user
		 * @return
		 */
	    public List<ProducePlan> findProducePlanList(ProducePlan producePlan) {
	        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
	        //designOption.getSqlMap().put("dsf", dataScopeFilter(user.getCurrentUser(), "o", "a"));
			return producePlanDao.findList(producePlan);
	    }
	    /**
		 * 查询一条采购预付款信息
		 * 
		 * @param user
		 * @return
		 */
	    public List<ProducePlan> findGroupList(){
	    	
	    	return producePlanDao.findGroupList();
	    }
	    
		/**
		 * 查询一条采购预付款信息
		 * 
		 * @param user
		 * @return
		 */
	    public ProducePlan getPlan(ProducePlan producePlan) {
	        
	        return producePlanDao.get(producePlan);
	        
	    }
	    /**
		 * ID查询一条数据
		 * 
		 * @param user
		 * @return
		 */
	    public ProducePlan findByID(Long id){
	    	
	    	return producePlanDao.get(id);
	    }
	    
	    /**
		 * 新增/修改一条数据
		 * @param user
		 */
	    @Transactional(readOnly = false)
	    public void saveProducePlan(ProducePlan producePlan) {
	    	// 判断新建编辑
	    		//若为null 执行插入语句（新建）
	    		if (producePlan.getId()==null){
	    			// 编辑
	    			StringBuilder conCode = new StringBuilder();
    				conCode.append("P");
    				conCode.append(DateUtils.formatDate(new Date(), "yyyyMMdd"));
    		        Sequence sequence=SequenceUtil.ognizationCode(conCode.toString(), "sequence_producePlan", 3);
    		        producePlan.setPlanNO(sequence.getReturnCode());
    		        producePlan.preInsert();
    		        producePlanDao.insert(producePlan);
	    				
	    		}else{
	    			// 更新数据
	    			producePlan.preInsert();
	    			producePlanDao.update(producePlan);
	    		}			
	    }
	    
	    /**
		 * 提交一条数据
		 * @param user
		 */
	    @Transactional(readOnly = false)
	    public void updateStatus(ProducePlan producePlan) {
	    			// 更新数据
	    		producePlan.preInsert();
	    		producePlan.setPlanStatus(1);
	    		producePlanDao.updateStatus(producePlan);
	    }
	    /**
		 * 删除一条数据
		 * @param user
		 */
	    @Transactional(readOnly = false)
	    public void deleteProducePlan(ProducePlan producePlan) {
	    	// 更新数据
	    		producePlan.preInsert();
	    		producePlan.setDeleteFlag("1");
	    		producePlanDao.delete(producePlan);
	    }
	    
	    /**
		 * 更新完成状态
		 * @param user
		 */
	    @Transactional(readOnly = false)
	    public void updateCompleteStatus(ProducePlan producePlan) {
	    	// 更新数据
	    		producePlan.preInsert();
	    		//设置状态为已完成
	    		producePlan.setPlanCompleteStatus("1");
	    		producePlanDao.updateCompleteStatus(producePlan);
	    }
	    
}
